package org.ccay.enumerable.loader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.ccay.enumerable.EnumItem;
import org.ccay.enumerable.PlainEnumItem;
import org.ccay.enumerable.SourceEnum;

/**
 * xml方式加载枚举类型
 * 配置文件示例：
	<pre>
	 &lt;?xml version="1.0" encoding="UTF-8"?&gt;
		&lt;enums&gt;
		    &lt;!--实例 :血型 --&gt;
		    &lt;enum type="bloodenum" &gt;
		        &lt;entry value="0" displayValue="A型" /&gt;
		        &lt;entry value="1" displayValue="B型" /&gt;
		        &lt;entry value="2" displayValue="O型" /&gt;
		        &lt;entry value="3" displayValue="AB型" /&gt;
		        &lt;entry value="4" displayValue="其他" /&gt;
		    &lt;/enum&gt;
		&lt;/enums&gt;
	 </pre>
 * @author chaowangbang
 *
 */
public class XmlEnumLoader implements EnumLoader{

	
	private static Log log = LogFactory.getLog(XmlEnumLoader.class);
	
	
	private Resource[] resources;
	
	public void setResources(Resource ... resources) {
		this.resources = resources;
	}

	public XmlEnumLoader(){}
	
	public XmlEnumLoader(Resource ... resources){
		this.resources = resources;
	}
	@Override
	public Map<String, List<EnumItem>> load() {
		Map<String, List<EnumItem>> result = new HashMap<String, List<EnumItem>>();
		for(Resource resource : resources){
			result.putAll(parseXml(resource));
		}
		return result;
	}

	private Map<String, List<EnumItem>> parseXml(Resource resource){
		Map<String, List<EnumItem>> result = new HashMap<String, List<EnumItem>>();
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(resource.getInputStream());
			Node root = document.getElementsByTagName("enums").item(0);
			NodeList cblist = root.getChildNodes();
			for (int i = 0; i < cblist.getLength(); i++) {
				Node cbinfo = cblist.item(i);
				if(!cbinfo.getNodeName().equals("enum")){
					continue;
				}
				String type = cbinfo.getAttributes().getNamedItem("type").getNodeValue();
				NodeList entrys = cbinfo.getChildNodes();
				for (int j = 0; j < entrys.getLength(); j++) {
					Node eninfo = entrys.item(j);
					if(!eninfo.getNodeName().equals("entry")){
						continue;
					}
					String value = eninfo.getAttributes().getNamedItem("value").getNodeValue();
					String displayValue = eninfo.getAttributes().getNamedItem("displayValue").getNodeValue();
					EnumItem item = new PlainEnumItem(type,null,value,displayValue,j,SourceEnum.SOURCE_XML.getValue());
					if(null == result.get(type)){
						result.put(type,  new ArrayList<EnumItem>());
					}
					result.get(type).add(item);
				}
			}
			log.info(resource.getFilename()+"解析完毕");
		} catch (FileNotFoundException e) {
			log.error(e.getMessage());
		} catch (ParserConfigurationException e) {
			log.error(e.getMessage());
		} catch (SAXException e) {
			log.error( e.getMessage());
		} catch (IOException e) {
			log.error( e.getMessage());
		}
		return result;
	}
}
